package org.glow.changgou.user.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.SneakyThrows;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Author Glow
 * @Date 2022-10-03 11:51:48
 * @Description mybatis-plus默认字段值设置
 * @Version 1.0
 */
@Component
public class MetaObjectHandlerConfig implements MetaObjectHandler {
    @SneakyThrows
    @Override
    public void insertFill(MetaObject metaObject) {
        Date currentDate = new Date();
        //默认未删除
        String timeStr1= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
       Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeStr1);
        setFieldValByName("created", date,metaObject);
        //创建时间默认当前时间
        setFieldValByName("updated", date,metaObject);
    }
    @Override
    public void updateFill(MetaObject metaObject) {
        Date currentDate = new Date();
        //修改时间
        setFieldValByName("modifyTime",currentDate,metaObject);
    }
}
